package com.roroldo.springcloud.alibaba.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@Slf4j
public class FlowLimitController {
    private AtomicInteger increase = new AtomicInteger(0);

    @GetMapping("/testA")
    public String testA() {
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB() {
        return "------testB";
    }

    @GetMapping("/testC")
    public String testC() {
        log.info("------测试流控效果，排队等候-----");
        return "------testC";
    }

    // 熔断测试
    @GetMapping("/testD")
    public String testD() {
        //暂停几秒钟线程
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("testD 测试RT");
        return "------testD";
    }

    @GetMapping("/testE")
    public String testE() {
        log.info("testE 测试RT");
        int old = increase.get();
        log.info(String.valueOf(old));
        if (old % 2 == 0) {
            increase.compareAndSet(old, old + 1);
            log.info("出现异常！");
            int age = 10 / 0;
        } else {
            increase.compareAndSet(old, old + 1);
            log.info("正常业务执行...");
        }
        return "------testE";
    }

    @GetMapping("/testF")
    public String testF() {
        log.info("testF 测试异常比例");
        int old = increase.get();
        log.info(String.valueOf(old));
        if (old % 2 == 0) {
            increase.compareAndSet(old, old + 1);
            log.info("出现异常！");
            int age = 10 / 0;
        } else {
            increase.compareAndSet(old, old + 1);
            log.info("正常业务执行...");
        }
        return "------testF 测试异常比例";
    }


}
